package com.synergy.stockflow.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.constant.ResultCode;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.product.entity.Material;
import com.synergy.stockflow.product.entity.MaterialProductClassification;
import com.synergy.stockflow.product.entity.UnitConversion;
import com.synergy.stockflow.product.service.BomService;
import com.synergy.stockflow.product.service.MaterialService;
import com.synergy.stockflow.product.service.UnitConversionService;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/material")
public class MaterialController {
    @Autowired
    private MaterialService materialService;
    @Autowired
    private UnitConversionService unitConversionService;

    @Autowired
    private BomService bomService;

    @GetMapping("/index")
    public Result<Page<Material>> page(@RequestParam(name = "current", defaultValue = "1") int current,
                                       @RequestParam(name = "size", defaultValue = "5") int size,
                                       @RequestParam(name = "materialName", defaultValue = "") String materialName,
                                       @RequestParam(name = "materialId", defaultValue = "") String materialId,
                                       @RequestParam(name = "materialOrProduct", defaultValue = "") String materialOrProduct,
                                       @RequestParam(name = "isEnabled", defaultValue = "") Integer isEnabled,
                                       @RequestParam(name = "category", defaultValue = "") String category
    ) {
        QueryWrapper<Material> query = new QueryWrapper<>();
        if (materialName != null && materialName.length() > 0) {
            query.lambda().like(Material::getMaterialName, materialName);
        }
        if (materialId != null && materialId.length() > 0) {
            query.lambda().like(Material::getMaterialId, materialId);
        }
        if (materialOrProduct != null && !materialOrProduct.equals("")) {
            query.lambda().like(Material::getMaterialOrProduct, materialOrProduct);
        }
        if (isEnabled != null) {
            query.lambda().like(Material::getIsEnabled, isEnabled);
        }
        if (category != null && category.length() > 0) {
            query.lambda().like(Material::getCategory, category);
        }
        query.lambda().orderBy(true,false, Material::getCreationTime);
        Page<Material> page = new Page<>(current, size);
        page = materialService.page(page, query);
//        List<Material> list = new ArrayList<>();
//        for (Material record : page.getRecords()) {
//            record.setBoms(bomService.selectspecificationsAndModels(record.getMaterialId()));
//        }
//        page.setRecords(list);
        return Result.success(page);
    }

    @GetMapping("/Name")
    public Result<Page<UnitConversion>> getName() {
        Page<UnitConversion> page = new Page<>(1, 10000);
        List<UnitConversion> list = unitConversionService.list();
        page.setRecords(list);
        return Result.success(page);
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody Material material) {
        QueryWrapper<Material> query = new QueryWrapper<>();
        if (material.getMaterialId() != null && material.getMaterialId().length() > 0) {
            query.lambda().eq(Material::getMaterialId, material.getMaterialId());
        }
        Page<Material> page = new Page<>(1, 10000);
        Page<Material> page1 = materialService.page(page, query);
        System.out.println(page1);
        if (page1 == null || page1.getRecords().size() == 0) {
            material.setCreationTime(new Date());
            materialService.save(material);
            return Result.success(material);
        }
        return Result.error(ResultCode.BUSINESS_EXCEPTION.value(), "物料编码已存在");

    }

    @PutMapping("/update")
    public Result update(@RequestBody Material material) {
        material.setUpdateTime(new Date());
        materialService.updateById(material);

        return Result.success(material);
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<String> materialIds) {
        materialService.removeBatchByIds(materialIds);
        return Result.success(materialIds);
    }
//    @PostMapping("/insertmaterial")
//    public Result insertmaterial(@RequestBody Material material) {
//        material.setCreationTime(new Date());
//        materialService.save(material);
//        return Result.success(material);
//
//    }
}